using UnityEngine;

public class FinalSwordObj : MonoBehaviour
{
    //方向
    public Vector2 dir;
    //速度
    public float speed;
    //销毁时间
    public float desTimer = 5;

    private void Update()
    {
        transform.Translate(dir*speed*Time.deltaTime);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        //碰到墙
        if(collision.gameObject.name=="Wall")
        {
            CreateEffect();
        }
        //碰到守塔人
        else if(collision.gameObject.GetComponent<TowerMan>()!=null)
        {
            //生命减少
            MsgFinalHurt msg = new MsgFinalHurt();
            msg.amount = 1;
            EventManager.AddMsg(msg);
            //生成特效
            CreateEffect();
            //碰撞声音
            AudManager.instance.PlayEnemyAud(AudManager.instance.enemyDic["受伤"], 1f);
        }
    }
    //设置方向
    public void SetDir(Vector2 _dir)
    {
        dir=_dir;
    }
    //生成特效
    public void CreateEffect()
    {
        GameObject newEff = PoolManager.Instance.GetNormalDieEffect();
        newEff.transform.position=transform.position;
        Destroy(gameObject);
    }
    //销毁自己
    public void UpdateDesTimer()
    {
        desTimer-=Time.deltaTime;

        if(desTimer<=0)
        {
            CreateEffect();
        }
    }
}